package com.dkd.manage.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.dkd.common.constant.DkdContants;
import com.dkd.common.core.domain.Location;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.LocationUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.domain.Node;
import com.dkd.manage.domain.VmType;
import com.dkd.manage.mapper.*;
import org.apache.poi.util.LocaleUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.domain.VendingMachine;
import com.dkd.manage.service.IVendingMachineService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备管理Service业务层处理
 * 
 * @author fuyuncheng
 * @date 2025-05-06
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService 
{
    @Autowired
    private VendingMachineMapper vendingMachineMapper;
    @Autowired
    private NodeMapper nodeMapper;
    @Autowired
    private VmTypeMapper vmTypeMapper;
    @Autowired
    private ChannelMapper channelMapper;

    /**
     * 查询设备管理
     * 
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id)
    {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    /**
     * 查询设备管理列表
     * 
     * @param vendingMachine 设备管理
     * @return 设备管理
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine)
    {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertVendingMachine(VendingMachine vendingMachine)
    {
        //新增设备
        vendingMachine.setCreateTime(DateUtils.getNowDate());
        vendingMachine.setInnerCode(UUIDUtils.getUUID());
        //根据设备类型id查询设备类型信息
        VmType vmType = vmTypeMapper.selectVmTypeById(vendingMachine.getVmTypeId());
        BeanUtils.copyProperties(vmType,vendingMachine);
        //根据设备点位id查询设备点位信息
        Node node = nodeMapper.selectNodeById(vendingMachine.getNodeId());
        BeanUtils.copyProperties(node,vendingMachine,"id");
        vendingMachine.setVmStatus(DkdContants.VM_STATUS_NODEPLOY);
        vendingMachine.setAddr(node.getAddress());
        //根据详细地址查询经纬度信息
        Location location = LocationUtils.getLocation(vendingMachine.getAddr());
        vendingMachine.setLongitudes(location.getLongitudes());
        vendingMachine.setLatitude(location.getLatitude());
        vendingMachine.setRunningStatus(DkdContants.TASK_STATUS_EXCEPTION);
        int result = vendingMachineMapper.insertVendingMachine(vendingMachine);
        //新增货道
        List<Channel> channels=new ArrayList<>();
        for (int i = 1; i <= vmType.getVmRow(); i++) {
            for (int j = 1; j <= vmType.getVmCol(); j++) {
                Channel channel =Channel
                        .builder()
                        .channelCode(i+"-"+j)
                        .vmId(vendingMachine.getId())
                        .innerCode(vendingMachine.getInnerCode())
                        .maxCapacity(vmType.getChannelMaxCapacity())
                        .currentCapacity(vendingMachine.getChannelMaxCapacity())
                        .build();
                channel.setCreateTime(DateUtils.getNowDate());
                channel.setUpdateTime(DateUtils.getNowDate());
                channels.add(channel);
            }
        }
        channelMapper.insertChannelList(channels);
        return result;
    }

    /**
     * 修改设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateVendingMachine(VendingMachine vendingMachine)
    {

        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        //如果修改点位后将点位对应的冗余字段的信息更新
        Node node = nodeMapper.selectNodeById(vendingMachine.getNodeId());
        vendingMachine.setNodeId(node.getId());
        vendingMachine.setAddr(node.getAddress());
        vendingMachine.setRegionId(node.getRegionId());
        vendingMachine.setPartnerId(node.getPartnerId());
        vendingMachine.setBusinessType(node.getBusinessType());
        return vendingMachineMapper.updateVendingMachine(vendingMachine);
    }

    /**
     * 批量删除设备管理
     * 
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids)
    {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 根据设备编号查询设备信息
     *
     * @param innerCode
     * @return
     */
    @Override
    public VendingMachine selectVendingMachineByInnerCode(String innerCode) {
        return vendingMachineMapper.selectVendingMachineByInnerCode(innerCode);
    }

    /**
     * 删除设备管理信息
     * 
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id)
    {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }
}
